package com.suitsoft.ut.commons;

import java.util.Map;
import java.util.Timer;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;




/**
 * 项目初始化器.
 * 
 * <pre>
 * 加载项目用到的公共数据到ServletContext中.
 * </pre>
 * 
 * @author Suineg
 */
public class AppLoaderListener implements ServletContextListener {
	private static final long serialVersionUID = 2861248186313165619L;

	/** 日志输出对象. */
	protected Logger logger = LoggerFactory.getLogger(getClass());
	/** 计时器，用来定时执行线程. */
	Timer timer = new Timer();

	/**
	 * 项目启动.
	 */
	public void contextInitialized(ServletContextEvent event) {
		logger.info("开始初始化应用程序...");
		@SuppressWarnings("unused")
		WebApplicationContext wac = null;
		ServletContext application = event.getServletContext();
		wac = WebApplicationContextUtils.getWebApplicationContext(application);
		AppConfig conf = AppConfig.getInstance();
//		CommonsDao commonsDao = (CommonsDao) wac.getBean("commonsDao");
		
	//	URL url = Offices.class.getClassLoader().getResource("offices.xml");
//		FileReader fr = null;
		try {
//			fr = new FileReader(url.getFile());
//			logger.debug("开始读取配置问文件:" + url.getFile());
//			JAXBContext context = JAXBContext.newInstance(Offices.class);
//			Unmarshaller um = context.createUnmarshaller();
//			Offices offices = (Offices) um.unmarshal(fr);
//			fr.close();
//			List<Office> officeList = offices.getOffice();
//			for(Office o : officeList) {
//				System.out.println(o);
//			}
//			conf.setOffices(offices);
			
			logger.info("初始化应用程序配置成功...");

			logger.info("开发初始化分页行数...");
			// 取得分页记录数
//			Map<String, Object> page = commonsDao.getPageSize("PAGE_SIZE");
			Map<String, Object> page = null;
			int pageSize = Constants.PAGE_SIZE;

			if (page != null) {
				pageSize = Integer.parseInt(String.valueOf(page.get("MEMO")));
			}else{
				logger.info("初始化分页失败！系统将使用系统默认分页行数！");
			}
			conf.setPageSize(pageSize);
			logger.info("初始化分页行数成功.");
			
			// 启动线程.
			// 使用线程删除 除今天以外的所有图片.
			logger.info("正在启动线程..");
			try {
				Constants.path = application.getRealPath("/") + Constants.SWF_TEMP_PATH;
				Constants.path1 = application.getRealPath("/") + Constants.PDF_TEMP_PATH;
//				timer.schedule(new DelTodayTempFileThread(), 0,Constants.INTERVAL * 1000 * 3600);
			} catch (Exception e) {
				timer.cancel();
				logger.info("线程启动失败.");
			}
			logger.info("线程启动成功.");

			application.setAttribute(Constants.SP, conf);

			logger.info("初始化应用程序成功...");
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("初始化应用程序配置失败:" + e.getMessage());
		}
//		} catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//			logger.error("初始化应用程序配置失败:" + e.getMessage());
//		} catch (JAXBException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//			logger.error("初始化应用程序配置失败:" + e.getMessage());
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//			logger.error("初始化应用程序配置失败:" + e.getMessage());
//		}
	}

	/**
	 * 项目关闭.
	 */
	public void contextDestroyed(ServletContextEvent event) {
		logger.info("正在关闭线程...");
		timer.cancel();
		logger.info("线程关闭成功.");
	}
}
